/*
9_ValidityBalanceTest.do

Makes placebo test and compliers tables

"Human Capital and Climate Change", 
	by Noam Angrist, Kevin Winseck, Harry Patrinos, and Joshua Graff Zivin.
		
Inputs:
	Cleaned_ESS_withFirstStage.dta
Outputs:
	BalanceTest_AnyReform.tex (Appendix Table A6)
	Compliers.tex (Appendix Table A4)
	Table2PanelA_placebo.tex (Appendix Table A7)

*/


global clustervar "cntryXlaw"

use "Cleaned_ESS_withFirstStage.dta", clear

drop if mi(minEdu)
* Drop if missing CSL data



* Clean the variables we want to check balance on
gen male = 1 if gndr==1
replace male = 0 if gndr==2

gen brncntr_recoded = 1 if brncntr==1
replace brncntr_recoded = 0 if brncntr==2


gen any_reform = .
foreach var of varlist reform_pos_sig_* {
	replace any_reform = `var' if any_reform != 1
}
tab any_reform

eststo clear

eststo: reg male any_reform yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 , vce(cluster $clustervar)
eststo: reg brncntr_recoded any_reform yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 , vce(cluster $clustervar)
*eststo: reg eduyrs_wins any_reform yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 , vce(cluster $clustervar)


esttab using "BalanceTest_AnyReform.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) /*scalars(Clusters Mean PercentChange FourYearEffect TimeTrend CountryFE ClusterVar)*/ keep(any_reform) label star(* 0.10 ** 0.05 *** 0.01) note(Standard errors in parentheses. P-values in brackets.) mtitles("Male" "Born in Country" /*"Years of Education"*/)

/*
eststo clear
local instrument "reform_pos_sig_* reform_pos_only_* reform_only_*"

eststo: reg male `instrument' yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 &!mi(beliefs_proenv), vce(cluster $clustervar)
local fstat = e(F)
local fstat: display%9.1f `fstat'
estadd local Fstatistic `fstat'
eststo: reg brncntr_recoded `instrument' yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 &!mi(beliefs_proenv) , vce(cluster $clustervar)
local fstat = e(F)
local fstat: display%9.1f `fstat'
estadd local Fstatistic `fstat'
eststo: reg eduyrs_wins `instrument' yrbrn  b2.cntry_numeric if in_sample==1 & in_pooled==1 &!mi(beliefs_proenv), vce(cluster $clustervar)
local fstat = e(F)
local fstat: display%9.1f `fstat'
estadd local Fstatistic `fstat'


esttab, scalars(Fstatistic)

local instrument "reform_pos_sig_* reform_pos_only_* reform_only_*"
local time_trend "yrbrn"
local cond "& in_pooled==1" 

ivregress 2sls brncntr_recoded (eduyrs_wins=`instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond' &!mi(beliefs_proenv), vce(cluster $clustervar)
ivregress 2sls male (eduyrs_wins=`instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond' , vce(cluster $clustervar)

*/



****************************** check compliers

gen complier = eduyrs_wins>= minEdu

levelsof cntry if in_sample==1 & in_pooled==1 & !mi(beliefs_proenv), local(countries)


file open tablefile using "Compliers.tex", write replace

file write tablefile "\begin{tabular}{lcc}" _n
file write tablefile " Country & Share Achieving Min.~CSL & Avg Education (Years)  \\ \hline " _n
*file write tablefile " Country & Proportion Achieving Minimum CSL & Avg Education (Years) & Green Vote Share \\ \hline " _n




foreach country in `countries'{
	su complier if in_sample==1 & in_pooled==1 & !mi(beliefs_proenv) & cntry=="`country'"
	local m1 = r(mean)
	local m1: display%9.3f `m1'
	
	su eduyrs_wins if in_sample==1 & in_pooled==1 & !mi(beliefs_proenv) & cntry=="`country'"
	local m2 = r(mean)
	local m2: display%9.3f `m2'
	
	/*su voting_proenv if in_sample==1 & in_pooled==1 & !mi(voting_proenv) & cntry=="`country'"
	local m3 = r(mean)
	local m3: display%9.3f `m3'
	*/
	
	
	
	file write tablefile "`country' & `m1' & `m2'  \\ " _n
	*file write tablefile "`country' & `m1' & `m2' & `m3' \\ " _n
	
}
file write tablefile "\hline \end{tabular}"
file close tablefile



******************* Placebo check
set seed 10232023
local instrument "reform_pos_sig_* reform_pos_only_* reform_only_*"


* Give respondent some random other treatment assignment
keep if in_sample==1 & in_pooled==1

generate id=_n
generate double u=runiform()
sort u
foreach var of varlist `instrument'{
	gen p_`var' = `var'[id]
}
gen p_cntryXlaw = cntryXlaw[id]
gen p_cntry_numeric = cntry_numeric[id]

local placebo_instrument "p_reform_pos_sig_* p_reform_pos_only_* p_reform_only_*"
local time_trend "yrbrn"
local cond "& in_pooled==1" 
*ivregress 2sls beliefs_proenv (eduyrs_wins=`placebo_instrument') `time_trend' i.cntry_numeric if in_sample==1 `cond', vce(cluster p_cntryXlaw)


********************************************************************************
****** Table 2 Outcome Indices with Linear Time Trends
********************************************************************************

* Panel A: above-median indicators
eststo clear

foreach outcome in beliefs_proenv behaviors_proenv policy_proenv voting_proenv {

	eststo: ivregress 2sls `outcome' (eduyrs_wins=`placebo_instrument') `time_trend' i.p_cntry_numeric if in_sample==1 `cond', vce(cluster p_cntryXlaw)
	gen in_reg_sample=e(sample)
	local nclust =  e(N_clust)
	local beta = _b[eduyrs_wins]
	su `outcome' if in_reg_sample==1
	local thismean: display%9.3f r(mean)
	local pct: display%9.1f `beta' / r(mean) * 100
	local fouryears: display%9.1f `beta' * 4 * 100
	drop in_reg_sample
	estadd local Clusters "`nclust'"
	estadd local Mean = "`thismean'"
	estadd local PercentChange = "`pct' \%"
	
	
}

esttab using "Table2PanelA_placebo.tex", replace cells("b(fmt(3))" se(par(`"("' `")"') fmt(3)) p(par(`"["' `"]"') fmt(3))) scalars(Clusters Mean PercentChange) keep(eduyrs_wins) label nostar nonote
